// 查询URL
const searchUrl = getUrl("xx/${moduleName}/${classMiniName}/listPage");
// 详情
const toDetailUrl = getWebURL("xx/${moduleName}/${classMiniName}/ppcshowdetails.html");
// 新增 修改URL
var toAddEditUrl = getWebURL("xx/${moduleName}/${classMiniName}/ppcaddorupdate.html");

// 删除URL
var delUrl = getUrl("xx/${moduleName}/${classMiniName}/delete");

//选择采购批次
var toBatchUrl = getWebURL("xx/${moduleName}/${classMiniName}/osplanpurchasebatch.html");

//导出URL
var exportUrl = getUrl("xx/${moduleName}/${classMiniName}/exportData") + "&actId=HH";

//var dictionaryUrl = getUrl('/master/rest/cmcdictionarydetails/listByDictionaryId');

var grid = new LayDataTable();
var ifameName;
$(function () {
    $("#filter").append(loadfilter($("input[name='menucode']").val()))
    autoCalcHeight();
    initLayuiSelect();
    // 初期化按钮事件
    initButton();
//	initSelectList();
    // 初期化table
    initTable();
});

function initLayuiSelect() {
    layui.use('form', function () {
        var form = layui.form;
        $("select[name='tabIndex']").empty();
        $("select[name='tabIndex']").append("<option value='all'>全部</option><option value='wait'>待发起</option><option value='have' >已发起</option>");

        $("select[name='tabIndex']").val('wait')
        initSelect('wait')
        
        setTimeout(function () {
            form.render('select');
        }, 5)
    })
}

//初期化按钮事件
function initButton() {
    layui.use('form', function () {
        var $ = layui.jquery,
            form = layui.form;

        form.on('select(tabIndex)', function (data) {
            var type = data.value;
            if ('wait' === type) {
                $("select[name = 'status']").val('');
            }
            initSelect(type)
            setTimeout(function () {
                form.render('select');
            }, 5)
        });
        // 查询
        $("#search").click(function () {
            grid.search();
        });
        // 查询
        $("#clear").click(function () {
            var arr = {
                tabIndex: 'wait'
            };
            grid.reset(arr);
            $("select[name='tabIndex']").val('wait');
            initSelect('wait')
            setTimeout(function () {
                form.render('select');
            }, 5)
        });
    })
    $("#selectBatchInfo").unbind('click').bind('click', function () {
        var url = toBatchUrl + "?nocache=" + new Date().getTime();
        dialog.showDialogNoBtn(url, "批次选择", '80%', '80%');
    })
}

function initSelect(type) {
    var selectValue = [];
    if ('wait' == type) {
        selectValue = [{code: "DFQ", value: "待发起"}];
    } else if ('have' == type) {
        selectValue = [
            {code: "BZZ", value: "编制中"},
            {code: "DSH", value: "待审核"},
            {code: "DTJBA", value: "待提交备案"},
            {code: "DXF", value: "待下发"},
            {code: "YXF", value: "已下发"}]
    } else {
        selectValue = [{code: "DFQ", value: "待发起"},
            {code: "BZZ", value: "编制中"},
            {code: "DSH", value: "待审核"},
            {code: "DTJBA", value: "待提交备案"},
            {code: "DXF", value: "待下发"},
            {code: "YXF", value: "已下发"}]
    }
    $("select[name='status']").empty()
    $("select[name='status']").append("<option value=''>请选择</option>")
    for (let item of selectValue) {
        $("select[name='status']").append("<option value='" + item.code + "'>" + item.value + "</option>")
    }
}

// table初始化
function initTable() {
    grid.init({
        elem: '#dataTable',
        url: searchUrl,
        toolbar: "#toolbarTable",
        cellMinWidth: 80,
        cols: [
            [{
                fixed: 'left',
                title: '操作',
                width: 100,
                toolbar: '#barTable',
                align: "center",
                sort: false,
                unresize: true
            }, {
                title: "序号",
                field: "RN",
                width: 80,
                fixed: 'left',
                type: 'numbers',
                sort: false,
                unresize: true
                }
                <#list records as rec>
              ,{
                 sort: false,
                 width: 200
                 field: '${rec.property}',
                 title: '${rec.comment}',
                 align: "center",
                 sort: true,
                 width: 140
               }
               </#list>
            ]
        ]
    });

    layui.use('table', function () {
        let table = layui.table;
        //头工具栏事件
        table.on('toolbar(dataTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    var url = toAddEditUrl;
                    dialog.show(url, "${tablecomment}XX");
                    break;
                case 'exports':
                    grid.exportExcel(exportUrl);
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                edit(data.id)
            } else if (obj.event === 'detail') {
                detail(data.id)
            }
        });
    });
}

// 更新
function edit(id) {
    var url = toAddEditUrl + "?id=" + id + "&nocache=" + new Date().getTime();
    dialog.show(url, "${tablecomment}维护");
}

// 详情
function detail(id) {
    var url = toDetailUrl + "?id=" + id + "&nocache=" + new Date().getTime();
    dialog.show(url, "查看${tablecomment}");
}

function sendData(data) {
    if (ifameName) {
        ifameName.addFormData.sonPageClick(data);
    }
}